<template>
  <div class="scheme-content">
    <div class="btn">
      <el-button type="primary" @click="addScheme">新增方案</el-button>
    </div>
    <el-table :data="tableData" border :height="height" style="width: 100%" :span-method="objectSpanMethod" resizable>
      <el-table-column label="方案名称" width="180">
        <template slot-scope="scope">
          <div>
            <div>
              <span style="margin: auto">{{ scope.row.planName }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="蓝方平台" prop="bluePlatform" width="230">
        <template slot-scope="{ row }">
          <div style="display: flex;flex-direction: column;text-align: left;" v-if="row.bluePlatform"
            v-for="(item, index) in row.bluePlatform.split('\n')" :key="index">
            {{ item }}
          </div>
          <div v-if="!row.bluePlatform"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button size="mini" @click="changeEditPlan(scope)" type="text">编辑</el-button>
          <el-button size="mini" type="text" @click="changeDeletePlan(scope)">删除</el-button>
          <el-button size="mini" type="text" @click="recommendScheme(scope)">推荐</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="padding-top: 24px" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="pageData.page" :page-sizes="[10, 20, 30, 50]" :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next" :total="pageData.total">
    </el-pagination>
  </div>
</template>

<script>
import { getPlanList, planDelete } from "@/api/project";
export default {
  data() {
    return {
      height: "calc(100vh - 350px)",
      checkAll: false,
      checkedCities: [],
      cities: [],
      isIndeterminate: true,
      estimate: false,
      data: [
        {
          value: "5",
          label: "评估",
          val: "ESTIMATING",
          des: "1.打击效果评估；2.战场态势评估；3.再次打击分析与决策",
        },
        {
          value: "1",
          label: "定位",
          val: "POSITIONING",
          des: "1.确定目标位置；2.掌握目标运动规律；3.对目标进行准确识别和查证",
        },
        {
          value: "2",
          label: "跟踪",
          val: "TRACKING",
          des: "1.调整侦察监视优先级；2.生成目标点轨迹，保持航迹连续性；3.按照生命周期，持续更新目标轨迹",
        },
        {
          value: "4",
          label: "打击",
          val: "STRIKING",
          des: "1.分发交战指令；2.启动武器发射；3.监视与管理战场态势；4.实时调整作战任务和打击预案",
        },
        {
          value: "3",
          label: "瞄准",
          val: "AIMING",
          des: "1.评估目标威胁；2.分析可用资源，进行交战规则制定；3.分析打击约束，生成打击预案；4.计算武器发射点/命中点；5.评估打击损益；6.生成决策质量",
        },
        {
          value: "0",
          label: "发现",
          val: "DISCOVERY",
          des: "1.情报收集，目标探测；2.传感器信息集成与融合；3.战场态势实时感知与动态更新",
        },
      ],
      pageData: {
        page: 1,
        size: 10,
        total: 0,
      },
      tableData: [],
      evaluation: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    switchActivityType(s) {
      let res = '';
      switch (s) {
        case 'DISCOVERY':
          res = '发现';
          break;
        case 'POSITIONING':
          res = '定位';
          break;
        case 'TRACKING':
          res = '跟踪';
          break;
        case 'AIMING':
          res = '瞄准';
          break;
        case 'STRIKING':
          res = '打击';
          break;
        case 'ESTIMATING':
          res = '评估';
          break;
      }
      return res;
    },
    handleSizeChange(val) {
      this.pageData.pageSize = val;
      this.pageData.page = 1;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageData.page = val;
      this.getData();
    },
    getData() {
      this.tableData = [];
      getPlanList({ taskId: ""})
        .then((res) => {
          const { code, data } = res?.data;
          if (code == 200) {
            data.forEach(item => {
              item.bluePlatform = item.bluePlatFormDesc;
            })
            this.tableData = data;
            // this.dataProcessing(data);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    changeEstimate() {
      this.estimate = true;
    },
    recommendScheme(scope){
      this.$router.push({
        path: "/app/RecommendScheme",
        query: {
          id: scope.row.planId,
        },
      });
    },
    changeDeletePlan(scope) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          planDelete({ planId: scope.row.planId })
            .then((res) => {
              const { code, msg } = res?.data;
              if (code == 200) {
                this.getData();
                this.$message.success(msg);
              } else {
                this.$message.error(msg);
              }
            })
            .catch((err) => {
              this.$message.error(err);
            });
        })
        .catch(() => { });
    },
    changeEditPlan(scope) {
      this.$router.push({
        path: "/app/editScheme",
        query: {
          id: scope.row.planId,
        },
      });
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.evaluation : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.evaluation.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.evaluation.length;
    },
    cancelProject() {
      this.estimate = false;
    },
    addScheme() {
      this.$router.push("/app/createScheme");
    },
    // eslint-disable-next-line
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 0 || columnIndex == 1 || columnIndex == 2) {
        // column.property是表头的key
        let spanArr = this.getSpanArr(this.tableData, "planId");
        const _row = spanArr[rowIndex];
        return {
          rowspan: _row,
          colspan: 1,
        };
      } 
    },
    getSpanArr(data, index) {
      let spanArr = [];
      let pos = "";
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          spanArr.push(1);
          pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i][index] === data[i - 1][index]) {
            spanArr[pos] += 1;
            spanArr.push(0);
          } else {
            spanArr.push(1);
            pos = i;
          }
        }
      }
      return spanArr;
    },
    getperformanceNum(c) {
      if (c.price == "greater") {
        return "大于" + c.num;
      } else if (c.price == "less") {
        return "小于" + c.num;
      } else {
        return "范围" + c.start + "~" + c.end;
      }
    },
    dataProcessing(data) {
      this.cities = [];
      this.evaluation = [];
      data.forEach((i) => {
        this.cities.push({
          value: i.planId,
          label: i.planName,
        });
        this.evaluation.push(i.planId);
        if (i.activities.length) {
          i.activities.forEach((j) => {
            if (j.platFormList && j.platFormList.length) {
              j.platFormList.forEach((k) => {
                if (k.platformDeviceList && k.platformDeviceList.length) {
                  k.platformDeviceList.forEach((e) => {
                    this.tableData.push({
                      planId: i.planId,
                      planName: i.planName,
                      planNum: i.planNum,
                      taskId: i.taskId,
                      planDes: i.planDes,
                      bluePlatform: i.bluePlatFormDesc,
                      //活动 activities
                      activityId: j.activityId,
                      activityNum: j.activityType,
                      activityType: this.switchActivityType(j.activityType),
                      activityDes: j.activityDes,
                      satisfied: j.satisfied,
                      //平台
                      platform: k.platform,
                      platformId: k.platformId,
                      equipmentName: k.equipmentName,

                      //设备
                      deviceName: e.deviceName,
                      capacityId: e.capacityId,
                      capacityName: e.capacityName,
                      performanceDesc: e.performanceDesc,

                    });
                  });
                } else {
                  this.tableData.push({
                    planId: i.planId,
                    planName: i.planName,
                    planNum: i.planNum,
                    taskId: i.taskId,
                    planDes: i.planDes,
                    bluePlatform: i.bluePlatFormDesc,
                    //活动 activities
                    activityId: j.activityId,
                    activityNum: j.activityType,
                    activityType: this.switchActivityType(j.activityType),
                    activityDes: j.activityDes,
                    satisfied: j.satisfied,
                    //平台
                    platform: k.platform,
                    platformId: k.platformId,
                    equipmentName: k.equipmentName,

                    //设备
                    deviceName: "",
                    capacityId: "",
                    capacityName: "",
                    performanceDesc: "",
                  });
                }
              })
            } else {
              this.tableData.push({
                planId: i.planId,
                planName: i.planName,
                planNum: i.planNum,
                taskId: i.taskId,
                planDes: i.planDes,
                bluePlatform: i.bluePlatFormDesc,
                //活动 activities
                activityId: j.activityId,
                activityNum: j.activityType,
                activityType: this.switchActivityType(j.activityType),
                activityDes: j.activityDes,
                satisfied: j.satisfied,
                //平台
                platform: "",
                platformId: "",
                equipmentName: "",

                //设备
                deviceName: "",
                capacityId: "",
                capacityName: "",
                performanceDesc: "",
              });
            }
          });
        } else {
          this.tableData.push({
            planId: i.planId,
            planName: i.planName,
            planNum: i.planNum,
            taskId: i.taskId,
            planDes: i.planDes,
            bluePlatform: i.bluePlatFormDesc,
            //活动 activities
            activityId: "",
            activityNum: "",
            activityType: "",
            activityDes: "",
            satisfied: null,
            //平台
            platform: "",
            platformId: "",
            equipmentName: "",

            //设备
            deviceName: "",
            capacityId: "",
            capacityName: "",
            performanceDesc: "",
          });
        }
      });
    },
    activityNumdes(val) {
      for (let i = 0; i < this.data.length; i++) {
        if (this.data[i].val == val) {
          return this.data[i].des;
        }
      }
    },
    activityNum(val) {
      for (let i = 0; i < this.data.length; i++) {
        if (this.data[i].val == val) {
          return this.data[i].label;
        }
      }
    },
    changeRelation(row) {
      if (row.relation == "IN") {
        return "范围" + row.attrValues.join("~") + row.unit;
      } else if (row.relation == "LESS") {
        return "小于" + row.attrValues.join("~") + row.unit;
      } else if (row.relation == "MORE") {
        return "大于" + row.attrValues.join("~") + row.unit;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.btn {
  margin-bottom: 16px;
}

.plan-edit:hover {
  color: #07f;
  cursor: pointer;
}

.plan-del:hover {
  color: #07f;
  cursor: pointer;
}
</style>